import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { AiModelTypeEnum, CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getSimpleKnowledgeList } from '#/api/ai/knowledge/knowledge';
import { getModelSimpleList } from '#/api/ai/model/model';
import { getToolSimpleList } from '#/api/ai/model/tool';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      component: 'Input',
      fieldName: 'formType',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.ai.model.chatRole.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      component: 'ImageUpload',
      fieldName: 'avatar',
      label: $t('views.ai.model.chatRole.avatar'),
      rules: 'required',
    },
    {
      fieldName: 'modelId',
      label: $t('views.ai.model.chatRole.modelId'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.modelIdPlaceholder'),
        api: () => getModelSimpleList(AiModelTypeEnum.CHAT),
        labelField: 'name',
        valueField: 'id',
        allowClear: true,
      },
      dependencies: {
        triggerFields: ['formType'],
        show: (values) => {
          return values.formType === 'create' || values.formType === 'update';
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'category',
      label:$t('views.ai.model.chatRole.category'),
      rules: 'required',
      dependencies: {
        triggerFields: ['formType'],
        show: (values) => {
          return values.formType === 'create' || values.formType === 'update';
        },
      },
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('views.ai.model.chatRole.description'),
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.descriptionPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'systemMessage',
      label: $t('views.ai.model.chatRole.systemMessage'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.systemMessagePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'knowledgeIds',
      label: $t('views.ai.model.chatRole.knowledgeIds'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.knowledgeIdsPlaceholder'),
        api: getSimpleKnowledgeList,
        labelField: 'name',
        mode: 'multiple',
        valueField: 'id',
        allowClear: true,
      },
    },
    {
      fieldName: 'toolIds',
      label: $t('views.ai.model.chatRole.toolIds'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.toolIdsPlaceholder'),
        api: getToolSimpleList,
        mode: 'multiple',
        labelField: 'name',
        valueField: 'id',
        allowClear: true,
      },
    },
    {
      fieldName: 'publicStatus',
      label: $t('views.ai.common.publicStatus'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: true,
      dependencies: {
        triggerFields: ['formType'],
        show: (values) => {
          return values.formType === 'create' || values.formType === 'update';
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'sort',
      label: $t('views.common.sort'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.common.sortPlaceholder'),
        class: 'w-full',
      },
      dependencies: {
        triggerFields: ['formType'],
        show: (values) => {
          return values.formType === 'create' || values.formType === 'update';
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      dependencies: {
        triggerFields: ['formType'],
        show: (values) => {
          return values.formType === 'create' || values.formType === 'update';
        },
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.ai.model.chatRole.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.namePlaceholder'),
      },
    },
    {
      fieldName: 'category',
      label: $t('views.ai.model.chatRole.category'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.ai.model.chatRole.categoryPlaceholder'),
      },
    },
    {
      fieldName: 'publicStatus',
      label: $t('views.ai.common.publicStatus'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.ai.common.publicStatusPlaceholder'),
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        allowClear: true,
      },
      defaultValue: true,
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('views.ai.model.chatRole.name'),
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.modelId'),
      field: 'modelName',
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.avatar'),
      slots: { default: 'avatar' },
      minWidth: 140,
    },
    {
      title: $t('views.ai.model.chatRole.category'),
      field: 'category',
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.description'),
      field: 'description',
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.systemMessage'),
      field: 'systemMessage',
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.knowledgeIds'),
      slots: { default: 'knowledgeIds' },
      minWidth: 100,
    },
    {
      title: $t('views.ai.model.chatRole.toolIds'),
      slots: { default: 'toolIds' },
      minWidth: 100,
    },
    {
      field: 'publicStatus',
      title: $t('views.ai.common.publicStatus'),
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
      minWidth: 80,
    },
    {
      field: 'status',
      title: $t('common.status'),
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
      minWidth: 80,
    },
    {
      title: $t('views.common.sort'),
      field: 'sort',
      minWidth: 80,
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
